﻿var InitialSearchQuery = '';
var CheckBool = '';
var Search_SelectedPositionID = "";
var Search_SelectedStateID = "";

$(function () {

    // Make the Position box a jQuery UI Combobox
    $("#SearchCombobox_Position").combobox({
        valueselect: function (event, ui) {
            Search_SelectedPositionID = ui.item.itemID;
            HandleFilters();
        },
        source: []
    });

    // Make the State box a jQuery UI Combobox
    $("#SearchCombobox_State").combobox({
        valueselect: function (event, ui) {
            Search_SelectedStateID = ui.item.itemID;
            HandleFilters();
        },
        source: []
    });

    $("#Search_PositionCombobox").find("input").attr("readonly", "readonly");
    $("#Search_StateCombobox").find("input").attr("readonly", "readonly");

    var position_Json;
    var state_Json;

    Search_SelectedPositionID = -1;
    Search_SelectedStateID = -1;

    $("#Search_PositionCombobox").removeClass("ui-state-error");
    $("#Search_StateCombobox").removeClass("ui-state-error");

    PageMethods.GetSearchData_Json(
        function (json) {
            position_Json = (eval(json)[0]);
            state_Json = (eval(json)[1]);

            // Send the data to the Positions Combobox
            $("#SearchCombobox_Position").combobox("option", "source", position_Json);
            // Send the data to the State Combobox
            $("#SearchCombobox_State").combobox("option", "source", state_Json);
        },
        function () {
            // TODO: SHOW ERROR MESSAGE
            $("#Search_PositionCombobox").addClass("ui-state-error");

            //set empty data
            $("SearchCombobox_Position").combobox("option", "source", []);

            // TODO: SHOW ERROR MESSAGE
            $("#SearchCombobox_State").addClass("ui-state-error");

            //set empty data
            $("#SearchCombobox_State").combobox("option", "source", []);
        }
    );

    $("#CandidateList").find(".HiddenActive").each(function (i) {
        if ($(this).val() === 'false') {
            $(this).parent(".IsActiveParent").addClass("ui-helper-hidden");
        }
    });

    $("#ActiveCheck").click(function () { //Handles CheckBox Clicks
        HandleFilters();
    });

    $("#NameFilter").keyup(function () { //Handles Filter Entries
        HandleFilters();
    });

    $("#CandidateList").menu();

    if (InitialSearchQuery.length != 0) {
        $("#NameFilter").removeClass("WatermarkInput");
        $("#NameFilter").val(InitialSearchQuery);
        $("#NameFilter").keyup();
    }

});

function HandleItemClick(candID) {
    window.location = CANDIDATE_PAGE_URL + '?candID=' + candID;
}

function HandleFilters() {
    var candidateList = $("#CandidateList").find("li");

    var defaultFilterTitle = $("#NameFilter").attr("title").toLowerCase();

    var bShowActiveOnly = $("#ActiveCheck").is(':checked');
    var FilterVal = $("#NameFilter").val().toLowerCase();

    candidateList.each(function (index) {
        var IsCandidateActive = $(this).find(".HiddenActive").val();
        var candidatePos = $(this).find(".HiddenPositionID").val();
        var candidateState = $(this).find(".HiddenStateID").val();
        var candidateEmail = $(this).find(".ItemEmail").html();
        var candidateName = $(this).find(".ItemName").html();
        var Bshown = true;

        if (candidateName.toLowerCase().indexOf(FilterVal) != -1 ||
             candidateEmail.toLowerCase().indexOf(FilterVal) != -1 ||
             FilterVal == defaultFilterTitle) { }
        else { Bshown = false; }

        if (IsCandidateActive == "true" || !bShowActiveOnly) { }
        else { Bshown = false; }

        if (candidatePos == Search_SelectedPositionID || Search_SelectedPositionID == -1) {}
        else { Bshown = false; }

        if (candidateState == Search_SelectedStateID || Search_SelectedStateID == -1) {}
        else { Bshown = false; }

        if (Bshown) { $(this).removeClass("ui-helper-hidden"); }
        else { $(this).addClass("ui-helper-hidden"); }
    });
}